#include <pthread.h>
#include <unistd.h>
#include <stdio.h>
#define   NUM_THREADS 10000

static int sum=0;
static pthread_mutex_t lock= PTHREAD_MUTEX_INITIALIZER;

void *thread_func(void *arg)
{
   pthread_mutex_lock(&lock);
   sum++;
   pthread_mutex_unlock(&lock);
    
}

int main(int argc, char const *argv[])
{
    pthread_t threads[NUM_THREADS];
    //创建月10000个线程累加sum
    for(int i=0;i<NUM_THREADS;i++)
    {
         pthread_create(&threads[i],NULL,thread_func,NULL);
    }


    for(int i=0;i<NUM_THREADS;i++)
    {     //等待线程结束
         pthread_join(threads[i],NULL );
    } 
    printf( "sum=%d\n",sum);
    pthread_mutex_destroy(&lock);
    return 0;
}
